<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;

class Power extends AdminController
{
    public function _initialize(){
        parent::_initialize();
    }
    // public function index()
    // {
    //     return view('admin@con1/tables');
    // }

    public function index()
    {
        // 读取用户列表
        $data = Db::table('sm_a_power')->paginate(7);
        $page = $data->render();
        // var_dump($data);
        return view('admin@Power/index',[
            'data' => $data,
            'title' => '我是显示节点页面',
            'page' => $page,
        ]);
    }

    public function add()
    {
        return view('admin@Power/add',[
            'title' => '我是添加节点页面'
        ]);
    }

    // 添加管理员
    public function insert(Request $request)
    {
        $data = $request->post();
        $d['Name'] = $data['Name'];
        $d['Mname'] = $data['Mname'];
        $d['Aname'] = $data['Aname'];
        $d['status'] = 0;
        $s = [
            'name' => $d['Name'],
            'mname' => $d['Mname'],
            'aname' => $d['Aname'],
            'status' => $d['status'],
        ];

        $result = Db('a_power')->insert($s);
        if ($result) {
            return $this->success('新增成功');
        } else {
            return $this->error('新增失败');
        }
    }

    public function delete($id)
    {
        // echo $id;
       $result = db('a_power')->delete($id);
       if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的节点删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的节点删除失败!';
        }
        return json($info);
    }

    public function edit($id)
    {
        $data = db('a_power')->find($id);
        return view('admin@Power/edit',[
            'title' => '我是修改ID:'.$id.'节点页面',
            'data' => $data,
            'id' => $id
        ]);
    }

    public function update(Request $request)
    {
        $data = $request->post();
        $id = $data['id'];
        $a = [
            'name' => $data['Name'],
            'mname' => $data['Mname'],
            'aname' => $data['Aname'],
            'status' => $data['go'],
            ];
        

        $result = Db('a_power')->where('id',$id)->update($a);

        if ($result) {
            return $this->success('编辑成功');
        } else {
            return $this->error('编辑失败');
        }
    }
    



}